package com.youku.expression.ast;

import com.youku.exception.RefErrorElementException;
import com.youku.expression.environment.Environment;

public class DotAst extends Ast {

	public DotAst(Ast obj, Ast field) {
		super(obj, field);
	}

	public Ast object() {
		return getChild(0);
	}

	public Ast field() {
		return getChild(1);
	}

	@Override
	public String toString() {
		return new StringBuilder().append('(').append(object()).append('.').append(field()).append(')').toString();
	}

	@Override
	public Object eval(Environment environment) {
		String obj = ((IdentifierAst) object()).getTokenValue();// 全局
		String field = ((IdentifierAst) field()).getTokenValue();// 局部

		Environment inner = environment.getOuter().getEnvironment(obj);

		if (inner == null) {
			throw new RefErrorElementException("不存在名为" + obj + "的测试用例");
		}

		return inner.get(field);
	}

}
